var home = require("../../utils/util.js");
var url = home.home;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active:0,
    memberId:home.getKey('userId'),//会员id
    page:1,
    storeId:home.getKey('storeId'),
    orderStatus:'null',
    list:[],
    show:false,
    bgcolor: home.getKey('bgcolor'),
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      active:parseInt(options.type)
    });
  },

  //获取订单列表
  getOrderList(){
    let that = this;
    wx.request({
      url: url + 'app/order/list',
      data:{
        storeId:home.getKey('storeId'),
        pageNo:that.data.page,
        memberId:home.getKey('userId'),
        orderStatus:that.data.orderStatus
      },
      method: 'GET',
      header: {
        "content-type": "application/json", // 默认
      },
      success:function (res) {
        if (res.data.code == 200) {
          let list = that.data.list;
          if (that.data.page == 1) {
            for (let i = 0; i < res.data.result.records.length; i++) {
              let json = JSON.parse(res.data.result.records[i].commodityListJsonStr);
              let type = 1;
              let typeArr = [];
              for (let j = 0; j < json.length; j++) {
                if (json[j].type == 2) {
                  typeArr.push(json[j].type);
                }
                json[j].totalPrice = (json[j].totalPrice - json[j].discountAmount).toFixed(2);
                if (json[j].pic) {
                  let pic = json[j].pic.split(',')[0];
                  json[j].pic = 'https://kyz.fbhkj.com//' + pic;
                }
              }
              if (typeArr.length == json.length) {
                type = 2;
              }
              res.data.result.records[i].type = type;
              res.data.result.records[i].arr = json;
              res.data.result.records[i].num = json.length;
            }
            list = res.data.result.records;
          } else {
            for (let i = 0; i < res.data.result.records.length; i++) {
              let type = 1;
              let typeArr = [];
              let json = JSON.parse(res.data.result.records[i].commodityListJsonStr);
              for (let j = 0; j < json.length; j++) {
                if (json[j].type == 2) {
                  typeArr.push(json[j].type);
                }
                json[j].totalPrice = (json[j].totalPrice - json[j].discountAmount).toFixed(2)
              }
              if (typeArr.length == json.length) {
                type = 2;
              }
              res.data.result.records[i].type = type;
              res.data.result.records[i].arr = json;
              res.data.result.records[i].num = json.length;
              list.push(res.data.result.records[i]);
            }
          }
          let show = false;
          if (list.length == 0) {
            show = true;
          }
          that.setData({show:show,list:list});
        } else {
          home.wxMessage('获取失败','none');
        }
      }
    })
  },

  //退款
  returnOrder(e){
    let that = this;
    wx.request({
      url: url + 'app/returnOrder',
      data:{
        orderId:e.currentTarget.dataset.orderid,
        storeId:home.getKey('storeId'),
      },
      method: 'POST',
      header: {
        "content-type": "application/json", // 默认
      },
      success:function (res) {
        if (res.data.code == 200) {
          home.wxMessage('已退款');
        } else {
          home.wxMessage(res.data.message,'none');
        }
        that.getOrderList();
      }
    })
  },

  onChange(e) {
    let orderStatus = '';
    if (e.detail.index == 1) {
      orderStatus = '0';
    } else if (e.detail.index == 2) {
      orderStatus = '1';
    } else if (e.detail.index == 3) {
      orderStatus = '2';
    } else {
      orderStatus = '';
    }
    this.setData({
      orderStatus:orderStatus,
      active:e.detail.index,
      page:1,
      list:[]
    });
    this.getOrderList();
  },

  /**
   * 跳转订单详情
   */
  toInfo(e){
    console.log(e);
    let goodsData = {};
    let list = this.data.list;
    for (let i = 0; i < list.length; i++) {
      if (list[i].id == e.currentTarget.dataset.orderid) {
        goodsData = list[i];
      }
    }
    goodsData = JSON.stringify(goodsData);
    wx.navigateTo({
      url: './complate?goodsData='+encodeURIComponent(goodsData),
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      list:[],
      page:1,
    })
    if (this.data.active == 0) {
      this.setData({
        orderStatus:'',
      })
    } else if (this.data.active == 1) {
      this.setData({
        orderStatus:'0',
      })
    }
    this.getOrderList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let page = this.data.page;
    this.setData({page:++page});
    this.getOrderList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})